1 /***
2 * Copyright 2003, 2004, 2005. CodeStreet LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.codestreet.selector.parser;
16
17 import java.util.Map;
18
19 /***
20 * Abstract base class for arithmetic expression evaluation operators.
21 *
22 * @author Jawaid Hakim.
23 */
24 abstract class ArithCompareOpImpl
25 {
26 /***
27 * Ctor.
28 *
29 * @param operator
30 * Operator.
31 */
32 private ArithCompareOpImpl(final String operator)
33 {
34 operator_ = operator;
35 }
36
37 public String toString()
38 {
39 return operator_;
40 }
41
42 public abstract Object eval(final Map identifiers, final IExpression lhs,
43 final IExpression rhs);
44
45 public abstract Object eval(final IValueProvider provider,
46 final Object corr, final IExpression lhs, final IExpression rhs);
47
48 /***
49 * = operator.
50 */
51 public static final ArithCompareOpImpl EQ = new ArithCompareOpImpl(" = ")
52 {
53 public Object eval(final Map identifiers, final IExpression lhs,
54 final IExpression rhs)
55 {
56 Object oLhs = lhs.eval(identifiers);
57 if (oLhs == null)
58 return Result.RESULT_UNKNOWN;
59 else if (!(oLhs instanceof NumericValue))
60 return Result.RESULT_FALSE;
61 NumericValue nLhs = (NumericValue) oLhs;
62
63 Object oRhs = rhs.eval(identifiers);
64 if (oRhs == null)
65 return Result.RESULT_UNKNOWN;
66 else if (!(oRhs instanceof NumericValue))
67 return Result.RESULT_FALSE;
68 NumericValue nRhs = (NumericValue) oRhs;
69
70 return (nLhs.doubleValue() == nRhs.doubleValue()) ? Result.RESULT_TRUE
71 : Result.RESULT_FALSE;
72 }
73
74 public Object eval(final IValueProvider provider, final Object corr,
75 final IExpression lhs, final IExpression rhs)
76 {
77 Object oLhs = lhs.eval(provider, corr);
78 if (oLhs == null)
79 return Result.RESULT_UNKNOWN;
80 else if (!(oLhs instanceof NumericValue))
81 return Result.RESULT_FALSE;
82 NumericValue nLhs = (NumericValue) oLhs;
83
84 Object oRhs = rhs.eval(provider, corr);
85 if (oRhs == null)
86 return Result.RESULT_UNKNOWN;
87 else if (!(oRhs instanceof NumericValue))
88 return Result.RESULT_FALSE;
89 NumericValue nRhs = (NumericValue) oRhs;
90
91 return (nLhs.doubleValue() == nRhs.doubleValue()) ? Result.RESULT_TRUE
92 : Result.RESULT_FALSE;
93 }
94 };
95
96 /***
97 * > operator.
98 */
99 public static final ArithCompareOpImpl GT = new ArithCompareOpImpl(" > ")
100 {
101 public Object eval(final Map identifiers, final IExpression lhs,
102 final IExpression rhs)
103 {
104 Object oLhs = lhs.eval(identifiers);
105 if (oLhs == null)
106 return Result.RESULT_UNKNOWN;
107 else if (!(oLhs instanceof NumericValue))
108 return Result.RESULT_FALSE;
109 NumericValue nLhs = (NumericValue) oLhs;
110
111 Object oRhs = rhs.eval(identifiers);
112 if (oRhs == null)
113 return Result.RESULT_UNKNOWN;
114 else if (!(oRhs instanceof NumericValue))
115 return Result.RESULT_FALSE;
116 NumericValue nRhs = (NumericValue) oRhs;
117
118 return (nLhs.doubleValue() > nRhs.doubleValue()) ? Result.RESULT_TRUE
119 : Result.RESULT_FALSE;
120 }
121
122 public Object eval(final IValueProvider provider, final Object corr,
123 final IExpression lhs, final IExpression rhs)
124 {
125 Object oLhs = lhs.eval(provider, corr);
126 if (oLhs == null)
127 return Result.RESULT_UNKNOWN;
128 else if (!(oLhs instanceof NumericValue))
129 return Result.RESULT_FALSE;
130 NumericValue nLhs = (NumericValue) oLhs;
131
132 Object oRhs = rhs.eval(provider, corr);
133 if (oRhs == null)
134 return Result.RESULT_UNKNOWN;
135 else if (!(oRhs instanceof NumericValue))
136 return Result.RESULT_FALSE;
137 NumericValue nRhs = (NumericValue) oRhs;
138
139 return (nLhs.doubleValue() > nRhs.doubleValue()) ? Result.RESULT_TRUE
140 : Result.RESULT_FALSE;
141 }
142 };
143
144 /***
145 * >= operator.
146 */
147 public static final ArithCompareOpImpl GE = new ArithCompareOpImpl(" >= ")
148 {
149 public Object eval(final Map identifiers, final IExpression lhs,
150 final IExpression rhs)
151 {
152 Object oLhs = lhs.eval(identifiers);
153 if (oLhs == null)
154 return Result.RESULT_UNKNOWN;
155 else if (!(oLhs instanceof NumericValue))
156 return Result.RESULT_FALSE;
157 NumericValue nLhs = (NumericValue) oLhs;
158
159 Object oRhs = rhs.eval(identifiers);
160 if (oRhs == null)
161 return Result.RESULT_UNKNOWN;
162 else if (!(oRhs instanceof NumericValue))
163 return Result.RESULT_FALSE;
164 NumericValue nRhs = (NumericValue) oRhs;
165
166 return (nLhs.doubleValue() >= nRhs.doubleValue()) ? Result.RESULT_TRUE
167 : Result.RESULT_FALSE;
168 }
169
170 public Object eval(final IValueProvider provider, final Object corr,
171 final IExpression lhs, final IExpression rhs)
172 {
173 Object oLhs = lhs.eval(provider, corr);
174 if (oLhs == null)
175 return Result.RESULT_UNKNOWN;
176 else if (!(oLhs instanceof NumericValue))
177 return Result.RESULT_FALSE;
178 NumericValue nLhs = (NumericValue) oLhs;
179
180 Object oRhs = rhs.eval(provider, corr);
181 if (oRhs == null)
182 return Result.RESULT_UNKNOWN;
183 else if (!(oRhs instanceof NumericValue))
184 return Result.RESULT_FALSE;
185 NumericValue nRhs = (NumericValue) oRhs;
186
187 return (nLhs.doubleValue() >= nRhs.doubleValue()) ? Result.RESULT_TRUE
188 : Result.RESULT_FALSE;
189 }
190 };
191
192 /***
193 * < operator.
194 */
195 public static final ArithCompareOpImpl LT = new ArithCompareOpImpl(" < ")
196 {
197 public Object eval(final Map identifiers, final IExpression lhs,
198 final IExpression rhs)
199 {
200 Object oLhs = lhs.eval(identifiers);
201 if (oLhs == null)
202 return Result.RESULT_UNKNOWN;
203 else if (!(oLhs instanceof NumericValue))
204 return Result.RESULT_FALSE;
205 NumericValue nLhs = (NumericValue) oLhs;
206
207 Object oRhs = rhs.eval(identifiers);
208 if (oRhs == null)
209 return Result.RESULT_UNKNOWN;
210 else if (!(oRhs instanceof NumericValue))
211 return Result.RESULT_FALSE;
212 NumericValue nRhs = (NumericValue) oRhs;
213
214 return (nLhs.doubleValue() < nRhs.doubleValue()) ? Result.RESULT_TRUE
215 : Result.RESULT_FALSE;
216 }
217
218 public Object eval(final IValueProvider provider, final Object corr,
219 final IExpression lhs, final IExpression rhs)
220 {
221 Object oLhs = lhs.eval(provider, corr);
222 if (oLhs == null)
223 return Result.RESULT_UNKNOWN;
224 else if (!(oLhs instanceof NumericValue))
225 return Result.RESULT_FALSE;
226 NumericValue nLhs = (NumericValue) oLhs;
227
228 Object oRhs = rhs.eval(provider, corr);
229 if (oRhs == null)
230 return Result.RESULT_UNKNOWN;
231 else if (!(oRhs instanceof NumericValue))
232 return Result.RESULT_FALSE;
233 NumericValue nRhs = (NumericValue) oRhs;
234
235 return (nLhs.doubleValue() < nRhs.doubleValue()) ? Result.RESULT_TRUE
236 : Result.RESULT_FALSE;
237 }
238 };
239
240 /***
241 * <= operator.
242 */
243 public static final ArithCompareOpImpl LE = new ArithCompareOpImpl(" <= ")
244 {
245 public Object eval(final Map identifiers, final IExpression lhs,
246 final IExpression rhs)
247 {
248 Object oLhs = lhs.eval(identifiers);
249 if (oLhs == null)
250 return Result.RESULT_UNKNOWN;
251 else if (!(oLhs instanceof NumericValue))
252 return Result.RESULT_FALSE;
253 NumericValue nLhs = (NumericValue) oLhs;
254
255 Object oRhs = rhs.eval(identifiers);
256 if (oRhs == null)
257 return Result.RESULT_UNKNOWN;
258 else if (!(oRhs instanceof NumericValue))
259 return Result.RESULT_FALSE;
260 NumericValue nRhs = (NumericValue) oRhs;
261
262 return (nLhs.doubleValue() <= nRhs.doubleValue()) ? Result.RESULT_TRUE
263 : Result.RESULT_FALSE;
264 }
265
266 public Object eval(final IValueProvider provider, final Object corr,
267 final IExpression lhs, final IExpression rhs)
268 {
269 Object oLhs = lhs.eval(provider, corr);
270 if (oLhs == null)
271 return Result.RESULT_UNKNOWN;
272 else if (!(oLhs instanceof NumericValue))
273 return Result.RESULT_FALSE;
274 NumericValue nLhs = (NumericValue) oLhs;
275
276 Object oRhs = rhs.eval(provider, corr);
277 if (oRhs == null)
278 return Result.RESULT_UNKNOWN;
279 else if (!(oRhs instanceof NumericValue))
280 return Result.RESULT_FALSE;
281 NumericValue nRhs = (NumericValue) oRhs;
282
283 return (nLhs.doubleValue() <= nRhs.doubleValue()) ? Result.RESULT_TRUE
284 : Result.RESULT_FALSE;
285 }
286 };
287
288 private final String operator_;
289 }
This page was automatically generated by Maven